<script setup>
import { inject, ref } from "vue";
import { onLoad, onReachBottom } from "@dcloudio/uni-app";
import { lowerList } from "@/services/team";
onLoad((e) => {
  type.value = e.type || "101";
  formdata.value.level_id = e.type || "101";
  if (e.type > 500) {
    formdata.value.type = 1;
  } else {
    formdata.value.type = 1;
  }

  getuserlist();
});
//下拉触底
onReachBottom(() => {
  getuserlist();
});
const formdata = ref({
  page: 1,
  limit: 10,
  keyword: "",
  type: "",
  level_id: "",
  month: "",
  agent_grade: "",
});
const type = ref("");
const list = ref([]);
const shownodate = ref(false);

const loading = ref(true);
const status = ref("more");

async function getuserlist() {
  loading.value = true;
  try {
    let res = await lowerList(formdata.value);
    if (res.code == 200) {
      // list.value = res.data.list;
      list.value = list.value.concat(res.data.list);
      shownodate.value = true;
      if (res.data.list.length < formdata.value.limit) {
        status.value = "no-more";
      } else {
        status.value = "more";
      }
      loading.value = false;
      if (res.data.list.length > 0) {
        formdata.value.page = formdata.value.page + 1;
      }
    }
  } catch (e) {
    uni.$u.toast(e.info);
  }
}

const keyword = ref("");
const search = () => {
  formdata.value.page = 1;
  list.value = [];
  getuserlist();
};

const date = ref(null);
// 日期选择器
const currentDate = getDate({
  format: true,
});

date.value = getDate({
  format: true,
});

const startDate = ref(null);
const endDate = ref(null);

startDate.value = getDate("start");
endDate.value = getDate({
  format: true,
});

function getDate(type) {
  const date = new Date();
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  // let day = date.getDate();

  if (type === "start") {
    year = year - 60;
  } else if (type === "end") {
    year = year;
  }
  month = month > 9 ? month : "0" + month;
  // day = day > 9 ? day : '0' + day;
  return `${year}-${month}`;
}

const DateSwitching = (Data, formString, type) => {
  let newdate = "";
  if (type == 1) {
    newdate = Data.split(formString);

    return newdate[0] + "年";
  }
  if (type == 2) {
    newdate = Data.split(formString);

    return newdate[0] + "年" + newdate[1] + "月";
  }
  if (type == 3) {
    newdate = Data.split(formString);

    return newdate[0] + "年" + newdate[1] + "月" + newdate[2] + "日";
  }
};
const goOpen = () => {
  uni.navigateTo({
    url: "/pages/team/shopkeeper/quotaVoucher?type=" + type.value,
  });
};
function bindDateChange(e) {
  formdata.value.page = 1;
  date.value = e.detail.value;
  formdata.value.month = e.detail.value;
  list.value = [];
  getuserlist();

  //   getChangeList(date.value);
}
const callphone = (item) => {
  uni.makePhoneCall({
    phoneNumber: item.ot_phone,
  });
};
</script>
<!-- <ClientListFilter /> -->
<template>
  <view class="content">
    <!-- filter -->

    <view class="content-shure">
      <u-search
        placeholder="搜索用户"
        :showAction="false"
        v-model="formdata.keyword"
        @search="search"
      ></u-search>
    </view>
    <picker
      mode="date"
      :value="date"
      :start="startDate"
      fields="month"
      :end="endDate"
      @change="bindDateChange"
    >
      <view class="content-time"
        ><text>{{ DateSwitching(date, "-", 2) }}</text
        ><image src="@/pages/team/static/ls-dowe.png" mode="scaleToFill"
      /></view>
    </picker>
    <view class="content-list" v-if="list.length > 0">
      <view
        class="content-list-item"
        v-for="(item, index) in list"
        :key="index"
      >
        <view class="content-list-item-top">
          <view class="content-list-item-name">
            <!-- <text>{{
              item.name ? item.name : item.nickname ? item.nickname : "---"
            }}</text> -->
            <text>{{ item.nickname ? item.nickname : "---" }}</text>
            <text
              class="lv-name"
              :class="item.level_id ? 'lv-name-' + item.level_id : 'lv-name-qy'"
              v-if="item.level && item.level.name"
              >{{ item.level && item.level.name ? item.level.name : "" }}</text
            >
          </view>
          <view class="content-list-item-phone" @click="callphone(item)">
            <text>{{ item.phone ? item.phone : "---" }}</text>
            <image src="@/pages/team/static/phone.png" mode="scaleToFill" />
          </view>
        </view>
        <view class="content-list-item-bt"
          >升级时间：{{
            item.level_upgrade_time ? item.level_upgrade_time : "---"
          }}</view
        >
      </view>
    </view>
    <uni-load-more color="#c6c5c3" :status="status" v-if="list.length > 0" />
  </view>
  <view class="nodeta" v-if="shownodate && list.length <= 0">
    <image src="@/static/nodate/datas.png" mode="widthFix" />
  </view>
  <view class="bt-footer">
    <view class="custom-style" @click="goOpen">为他人开通</view>
  </view>
</template>

<style lang="scss" scoped>
@import "./index.scss";
</style>
